package org.jeecg.modules.meta.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.metasyn.entity.FdColumn;
import org.jeecg.metasyn.entity.FdTable;
import org.jeecg.modules.meta.entity.SysFieldinfo;
import org.jeecg.modules.meta.entity.SysTableinfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("metaSynDBOperAllServiceImpl")
public class MetaSynDBOperAllServiceImpl extends AbstractMetaSynDBOperService {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Override
    void printLog(Date date) {
        log.info("执行元数据同步全量操作!");
    }

    @Override
    List<FdTable> synMetaTablePage(long current, long size, Date date) {
        IPage<FdTable> page = new Page(current, size);
        //获取元数据平台表数据
        IPage<FdTable> tableList = fdTableMapper.selectPage(page, new QueryWrapper<>());
        if (null == tableList || tableList.getRecords().size() == 0) {
            return new ArrayList<>();
        }

        return tableList.getRecords();
    }

    @Override
    List<FdColumn> synMetaFieldPage(long current, long size,Date date) {
        IPage<FdColumn> page = new Page(current, size);
        //获取元数据平台字段数据
        IPage<FdColumn> columnList = fdColumnMapper.selectPage(page, new QueryWrapper<>());

        if (null == columnList || columnList.getRecords().size() == 0) {
            return new ArrayList<>();
        }
        return columnList.getRecords();
    }

    @Override
    void updateStatusByTime(Date date) {
        //获取待删除数据
        List<SysTableinfo> dels = getDelListByTime(date);
        if (null == dels || dels.size() == 0) {
            return;
        }
        //删除元数据表同步的操作
        updateTableInfoStatusByTime(dels);
        //删除自动订阅对应的表信息 todo 暂时不删除
    }

    private void updateTableInfoStatusByTime(List<SysTableinfo> systbainfos) {
        for (SysTableinfo sysTableinfo : systbainfos) {
            sysTableinfo.setFdStatus("2");
            iSysTableinfoService.updateById(sysTableinfo);
        }
    }

    private List<SysTableinfo> getDelListByTime(Date date) {
        QueryWrapper<SysTableinfo> wrapper = new QueryWrapper<>();
        wrapper.le("UPDATE_TIME", date);
        List<SysTableinfo> systbainfos = sysTableinfoMapper.selectList(wrapper);
        if (null == systbainfos || systbainfos.size() == 0) {
            return null;
        }
        return systbainfos;
    }



    //删除一分钟之前得本次没有更新的数据，逻辑删除状态变成2
    @Override
    void updateFieldStatusByTime(Date date) {
        //获取待删除数据
        List<SysFieldinfo> dels = getDelFieldListByTime(date);
        if (null == dels || dels.size() == 0) {
            return;
        }
        //删除元数据表同步的操作
        updateFieldStatusByTime(dels);
    }

    private void updateFieldStatusByTime(List<SysFieldinfo> sysFieldinfos) {
        for (SysFieldinfo sysFieldinfo : sysFieldinfos) {
            sysFieldinfo.setFdStatus("2");
            iSysFieldinfoService.updateById(sysFieldinfo);
        }
    }

    private List<SysFieldinfo> getDelFieldListByTime(Date date) {
        QueryWrapper<SysFieldinfo> wrapper = new QueryWrapper<>();
        wrapper.le("UPDATE_TIME", date);
        List<SysFieldinfo> sysFieldinfos = sysFieldinfoMapper.selectList(wrapper);
        if (null == sysFieldinfos || sysFieldinfos.size() == 0) {
            return null;
        }
        return sysFieldinfos;
    }


}
